package com.dji.common.api;

import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dji.bean.Compoment;
import com.dji.common.utils.PropertiesUtil;

@Compoment
public class DateAPI {
	private final static Logger log = LoggerFactory.getLogger(DateAPI.class);

	/**
	 * 获取外网时间
	 * 
	 * @return
	 */
	public static Date getInternateDateTime() {
		String dateTimeServerList = PropertiesUtil.getString("dateTime.server");
		for (String dateTimeServerURL : dateTimeServerList.split(";")) {
			if (!"".equals(dateTimeServerURL)) {
				String httpDate = null;
				try {
					URL url = new URL(dateTimeServerURL);
					URLConnection uc = url.openConnection();
					uc.connect();
					return new Date(uc.getDate());// 区域化的时间,详见jdk解释the
													// time zone
													// of the Java virtual
													// machine running the
													// application
				} catch (Exception e) {
					log.debug("从网路获取时间失败：" + httpDate + e.getMessage());
				}
			}
		}
		return null;
	}
}
